<?php

class grid
{
    var $pageSize;
    var $pageNo;
    var $totalCount;
    var $thisCount;
    var $result;
    var $columns = array();
    var $fields = array();
    var $navtable = "";
    var $navdiv = "";
    var $orderby = "";
    var $whereCondition = "";
    var $sqlstr = "";
    var $countSql = "";
    var $query_all_sql = "";
    var $navType = 0;
    var $debug = false;
    var $useCache = true;
    var $navPageRange = true;

    var $db_id = "default";

    public function __construct()
    {
        $this->pageSize = 16;
        $this->pageNo = 1;
        $this->pageCount = 1;
        $this->totalCount = 0;
        $this->thisCount = 0;
    }

    function set_order($ordstr)
    {
        $this->orderby = $ordstr;
    }

    function set_navtype($type)
    {
        $this->navType = $type;
    }

    function set_db_id($db_id)
    {
        $this->db_id = $db_id;
    }

    function footer_table($object)
    {
        $row_1 = $this->pageNo * $this->pageSize + 1;
        $row_2 = $row_1 + $this->pageSize - 1;

        if ($row_2 > $this->totalCount) {
            $row_2 = $this->totalCount;
        }

        $navstr = "";
        $navstr .= "<table class='table_nav'>";
        $navstr .= "<tr class='tr_foot'>";
        if ($this->navPageRange) {
            $navstr .= "<td class='td_nav_l'>($row_1-$row_2)/$object->totalCount</td>";
        }

        $navstr .= "<td class='td_nav_r'>";
        $navstr .= "<ul class='grid-pagination'>";
        $current_pageNo = $object->pageNo;
        if ($object->navType == 0) {
            if ($object->pageCount <= 1) {
                //
            } else {
                if ($object->pageNo == 0) {
                    $navstr .= "<li onclick='javascript:void(0);'>首页</li>";
                } else {
                    $navstr .= "<li onclick='grid_nav(0)'>首页</li>";
                }
                if ($object->pageNo == 0) {
                    $navstr .= "<li onclick='javascript:void(0);'>上一页</li>";
                } else {
                    $navstr .= "<li onclick='grid_nav(" . ($object->pageNo - 1) . ")'>上一页</li>";
                }

                $no1 = $object->pageNo - 5;
                $no2 = $object->pageNo + 5;

                if ($object->pageCount > 1) {
                    for ($n = $no1; $n <= $no2; $n++) {
                        if ($n < 0) {
                            continue;
                        }
                        if ($n >= $object->pageCount) {
                            continue;
                        }
                        $no = $n + 1;
                        if ($n == $object->pageNo) {
                            $navstr .= "<li class='pagination-current'>" . ($no) . "</li>";
                        } else {
                            $navstr .= "<li onclick=\"grid_nav('$n')\">" . ($no) . "</li>";
                        }
                    }
                }


                if ($object->pageNo == ($object->pageCount - 1)) {
                    $navstr .= "<li onclick='javascript:void(0);'>下一页</li>";
                } else {
                    $navstr .= "<li onclick='grid_nav(" . ($object->pageNo + 1) . ")'>下一页</li>";
                }
                if ($object->pageNo == ($object->pageCount - 1)) {
                    $navstr .= "<li onclick='javascript:void(0);'>尾页</li>";
                } else {
                    $navstr .= "<li onclick='grid_nav(" . ($object->pageCount - 1) . ")'>尾页</li>";
                }
            }
        }
        $navstr .= "</ul>";
        $navstr .= "</td>";
        $navstr .= "</tr>";
        $navstr .= "</table>";
        $navstr .= "";

        return $navstr;

    }

    function init($sqlstr, $page_no, $page_size)
    {
        global $configs;
        global $dbs;
        global $SCRIPT_NAME;

        $this->pageSize = $page_size;
        $this->pageNo = $page_no;
        $this->result = array();

        if ($this->pageSize > 0) {
            if (empty($this->countSql)) {
                $this->countSql = "SELECT count(0) AS sl FROM (" . $sqlstr . ") t";
            }
            if ($this->debug) {
                $this->print_html("count sqlstr is: <br> " . $this->countSql);
            }
            $this->totalCount = query_col00($this->countSql, $this->db_id);
        }

        if ($this->pageSize > 0) {
            $this->pageCount = ceil($this->totalCount * 1.0 / $this->pageSize);
            $this->pageCount = $this->pageCount == 0 ? 1 : $this->pageCount;
        }

        if ($this->pageNo >= $this->pageCount && $this->pageCount > 0) {
            $this->pageNo = 0;
        }

        $query = $sqlstr;
        if (isset($this->orderby) && $this->orderby != "") {
            $query = $query . " order by " . $this->orderby;
        }

        $this->query_all_sql = $query . "";

        if ($this->pageSize > 0) {
            $begin = $this->pageNo * $this->pageSize;
            $query = $query . " limit $begin," . $this->pageSize;
        }

        $this->sqlstr = $query;
        if ($this->debug) {
            $this->print_html($this->sqlstr);
        }


        $grid_refresh = RequestUtils::get("grid_refresh", true);

        $cache_key = md5($query);
        $this->result = [];
        if ($this->useCache == true && $grid_refresh == "false" && MemcacheUtils::enabled()) {
            if (MemcacheUtils::exists($cache_key)) {
                // echo "get from cache";
                $this->result = MemcacheUtils::get($cache_key);
            }
        }
        if (empty($this->result)) {
            $this->result = @query_result($query, $this->db_id, MYSQL_ASSOC);
            MemcacheUtils::cache($cache_key, $this->result, false, 300);
        }


        $this->thisCount = count($this->result);
        if ($this->pageSize <= 0) {
            $this->totalCount = count($this->result);
            $this->pageSize = $this->totalCount;
        }

        if (count($this->result) > 0) {
            while (list($key, $value) = each($this->result[0])) {
                $column = strtolower($key);
                array_push($this->columns, $column);
                $this->fields[$column] = $key;
            }
        }


        $this->navtable = $this->footer_table($this);
        return;
    }

    function print_html($info)
    {
        echo "<br> $info ";
    }

    function get_sum($column)
    {
        $total = 0;
        for ($i = 0; $i < $this->fetch_rows(); $i++) {
            if ($this->get($i, $column) == "") {
                continue;
            }
            $total += $this->get($i, $column) * 1;
        }
        return $total;
    }

    function get_time($rowIndex, $column)
    {
        $value = $this->get($rowIndex, $column);
        if ($value == null || $value == "") {
            return "";
        }
        return substr($value, 0, 16);
    }

    function get_short_time($rowIndex, $column)
    {
        $value = $this->get($rowIndex, $column);
        if ($value == null || $value == "") {
            return "";
        }
        return substr($value, 5, 11);
    }

    function get_date($rowIndex, $column)
    {
        $value = $this->get($rowIndex, $column);
        if ($value == null || $value == "") {
            return "";
        }
        return substr($value, 0, 10);
    }

    function get($rowIndex, $column)
    {
        if ($this->totalCount == 0 || $this->totalCount < $rowIndex) {
            return "out of row index $rowIndex [" . $this->sqlstr . "]";
        }
        $name = $this->get_field_name($column);
        if ($name == "") {
            return "unknow column $column";
        }
        return $this->result[$rowIndex][$name];
    }

    function get_line($index)
    {
        if (count($this->result) > $index) {
            return $this->result[$index];
        }
        return false;
    }

    function get_column_into_matrix($column)
    {
        $matrix = array();
        for ($i = 0; $i < $this->fetch_rows(); $i++) {
            $one = $this->get($i, $column);
            if ($one != "") {
//				$matrix[$i] = $one;
                array_push($matrix, $one);
            }
        }
        return $matrix;
    }

    function get_column_into_list($column)
    {
        return $this->get_column_into_matrix($column);
    }

    function col($rowIndex, $colIndex)
    {
        $column = $this->get_field_name_by_index($colIndex);
        return $this->get($rowIndex, $column);;
    }

    function get_field_name_by_index($colIndex)
    {
        $matrix = array_values($this->fields);
        if ($colIndex >= count($matrix)) {
            return "out of index $colIndex";
        }
        return $matrix[$colIndex];
    }

    function columns()
    {
        return $this->columns;
    }

    function get_field_name($name)
    {
        $temp = strtolower($name);
        if (array_key_exists($temp, $this->fields)) {
            return $this->fields[$temp];
        }
        return "";
    }

    function fetch_rows()
    {
        return $this->thisCount;
    }

    function get_data()
    {
        return $this->result;
    }

    function get_span()
    {

    }

    function nav_table()
    {
        return $this->navtable;
    }

    function nav_div()
    {
        return $this->navdiv;
    }

    function show_empty_rows($n)
    {
        echo $this->get_empty_rows($n);
    }

    function set_debug($debug)
    {
        $this->debug = $debug;
    }

    function get_empty_rows($n)
    {
        global $configs;
        $str = '';
        $len = $this->pageSize - $this->thisCount;
        for ($i = 0; $i < $len; $i++) {
            $str .= "<tr>";
            for ($t = 0; $t < $n; $t++) {
                $str .= "<td>&nbsp;</td>";
            }
            $str .= '</tr>' . "\r\n";
        }
        return $str;
    }

    function set_count_sql($sql)
    {
        $this->countSql = $sql;
    }

    /**
     * @return string
     */
    public function getSqlstr()
    {
        return $this->sqlstr;
    }

    /**
     * @param string $sqlstr
     */
    public function setSqlstr($sqlstr)
    {
        $this->sqlstr = $sqlstr;
    }

    /**
     * @return boolean
     */
    public function isUseCache()
    {
        return $this->useCache;
    }

    /**
     * @param boolean $useCache
     */
    public function setUseCache($useCache)
    {
        $this->useCache = $useCache;
    }

    /**
     * @return string
     */
    public function getQueryAllSql()
    {
        return $this->query_all_sql;
    }

    /**
     * @param string $query_all_sql
     */
    public function setQueryAllSql($query_all_sql)
    {
        $this->query_all_sql = $query_all_sql;
    }

    /**
     * @return boolean
     */
    public function isNavPageRange()
    {
        return $this->navPageRange;
    }

    /**
     * @param boolean $navPageRange
     */
    public function setNavPageRange($navPageRange)
    {
        $this->navPageRange = $navPageRange;
    }

    /**
     * @return mixed
     */
    public function getResult()
    {
        return $this->result;
    }

    /**
     * @param mixed $result
     */
    public function setResult($result)
    {
        $this->result = $result;
    }


}

